package it.service.ipml;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import it.mapper.StudentMapper;
import it.pojo.Student;
import it.pojo.StudentExample;
import it.pojo.StudentExample.Criteria;
import it.service.IStudentService;

@Service
public class StudentServiceIpml implements IStudentService {

	@Autowired
	private StudentMapper studentMapper;

	public Map<String, String> createUser(Student student) {
		// TODO Auto-generated method stub

		// 判断数据的可用性
		Student _student = null;
		List<Student> students = null;
		Map<String, String> map = new HashMap<String, String>();
		// 判断学号是否被注册
		_student = this.studentMapper.selectByPrimaryKey(student.getNumber());
		if (null != _student) {
			map.put("number", "学号已经被注册");
		}
		StudentExample example = new StudentExample();
		Criteria criteria = example.createCriteria();

		String phone = student.getPhone();
		String qq = student.getQq();

		// 判断手机号是否被注册
		criteria.andPhoneEqualTo(phone);
		students = this.studentMapper.selectByExample(example);
		if (null != students && !students.isEmpty()) {
			map.put("phone", "手机号已经被注册");
		}
		// 判断qq是否被注册
		criteria.andQqEqualTo(qq);
		students = this.studentMapper.selectByExample(example);
		if (null != students && !students.isEmpty()) {
			map.put("qq", "QQ号已经被注册");
		}
		if (null != map && !map.isEmpty()) {
			return map;
		}
		// 设置注册时间
		student.setCreatetime(new Date());
		this.studentMapper.insert(student);
		return map;
	}

	public List<Student> getStudentList() {
		StudentExample example = new StudentExample();
		Criteria criteria = example.createCriteria();
		List<Student> students = this.studentMapper.selectByExample(example);
		return students;

	}

}
